//题意理解错误，以“在原数组操作”的前提下，把“删除相同元素”理解为删除该元素后，将数组内其他元素依次前移； 假设该数组有5个相同元素，则最终的得到一个末5位为0，前5位为其他数按照原顺序排列的数。
//在代码中，首先假设10个元素内之多只有1个相同的值，首先录入10个元素，然后查找录入的10个元素中哪个值是重复的，并计算有几个重复的值，然后对原数组进行如上所述的操作。
//虽然跑题了，但相比本来题意复杂度只增不减，如果引入另外一个数组操作每一步的逻辑都会变得相当简单，所以保留原答案不改。
package com.bootcamp.java;

import sun.security.util.Length;

import javax.lang.model.element.VariableElement;
import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.ForkJoinPool;

/**
 * @program: ArrayCode
 * @description: assume that there is at most one value that has more than one element,
 * other elements has different values
 * @author: Keyu Li
 * @create: 2021-08-26 19:51
 **/

public class DeleteSameValue {
    public static void main(String[] args) {
        int[] arr = new int[10];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 10; i++) {
            System.out.print("please input the "+(i+1)+"/10 integer: ");
            arr[i] = Integer.parseInt(sc.nextLine());
        }
        System.out.println("your origin array is: \n" + Arrays.toString(arr));
        // create an array to store numbers and value.
        int[] numberAndValue = deleteSameValue(arr);
        if (numberAndValue[0] == 0) {
            System.out.println("No same value found!");
            return;
        }
        System.out.println("the array has " + numberAndValue[0] + " elements of value " + numberAndValue[1]);
        System.out.println("your final array is: \n" + Arrays.toString(arr));
    }

    private static int[] deleteSameValue(int[] arr) {
        // index 0 store numbers and index 1 store the value.
        int[] numberAndValue = {0, 0};
        int[] sameValueIndex = new int[10];
        // find which value has more than 1 element
        int count = 1;
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = i+1; j < arr.length; j++) {
                if (arr[i]==arr[j]){
                    sameValueIndex[count] = j;
                    count++;
                }
            }
            if (count!=1){
                sameValueIndex[0] = i;
                numberAndValue[0] = count;
                numberAndValue[1] = arr[i];
                break;
            }
        }
        // array has no same value.
        if ( numberAndValue[0]==0 )
            return numberAndValue;
        // delete the same value element and move others forward.
        int i = 0;
        for (int j = sameValueIndex[i]; j < arr.length; j++) {
            if (j==sameValueIndex[i]) {
                arr[j]=0;
                i++;
                continue;
            }
            arr[j-i] = arr[j];
            arr[j] = 0;

        }
        return numberAndValue;
    }
}
